package com.qin.entity.subcomponents;

import com.qin.common.RegexProcessor;
import com.qin.entity.ExecuteBean;
import com.qin.entity.ExecuteLog;
import com.qin.service.ExecuteService;
import com.qin.service.impl.ExecuteServiceImpl;

public class RegexExtractSubbComponent {

    //所属组件ID
    private String component_id;
    //正则表达式
    private String regex_text;
    //赋值变量名
    private String regex_assign_paraname;

    private ExecuteService executeService = new ExecuteServiceImpl();

    public RegexExtractSubbComponent(String component_id, String regex_text, String regex_assign_paraname) {
        this.component_id = component_id;
        this.regex_text = regex_text;
        this.regex_assign_paraname = regex_assign_paraname;
    }

    public void execute(ExecuteBean executeBean){

        //设置ExecuteLog对象的各属性
        ExecuteLog executeLog = new ExecuteLog(executeBean.getExecute_id(), executeBean.getFunction_id(),
                executeBean.getTestcase_id(), executeBean.getComponent_id(), executeBean.getSubcomponent_id(), "");

        RegexProcessor regexProcessor = new RegexProcessor();
        //获取父接口组件的调用结果
        String inputString = executeBean.getInterfaceResultMap().get(component_id);

        //获取正则匹配结果的第一个
        String regex_assign_paravalue = null;
        if(regexProcessor.getStringByRegex(inputString, regex_text).size() > 0){
            regex_assign_paravalue = regexProcessor.getStringByRegex(inputString, regex_text).get(0);
        }

        //插入日志
        executeService.insertExecuteLog(executeLog, ">>>>>正则变量提取：输入的正则表达式=" + regex_text);
        executeService.insertExecuteLog(executeLog, ">>>>>正则变量提取：提取变量名=" + regex_assign_paraname + "，提取变量值=" + regex_assign_paravalue);

        //把queryResultList添加到executeBean的dbQueryResultMap中
        executeBean.getParaMap().put(regex_assign_paraname, regex_assign_paravalue);
    }
}
